// components/goodsChoice/index.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Component({

    /**
     * 页面的初始数据
     */
    properties: {
        meIsShow: {
            type: Boolean,
            value: true
        },
        num: {
            type: Number | String,
            value: 4
        },
        typeVa: {
            type: String,
            value: ''
        },
    },

    data: {
        typeValue: 'vip',
        topGoodsList: [],
        is_vip_price: JSON.parse(wx.getStorageSync('is_vip_price') || 'false'),
        size: 4,
        total: 0,
        is_shop: 0,
    },

    pageLifetimes: {
        show: function () {
            // 页面被展示
            var that = this

            this.setData({
                is_shop: wx.getStorageSync('is_shop') || 0,
            })

            if (wx.getStorageSync('user_id3')) {
                //用户授权登录
                app.login(function (userInfo) {

                    that.setData({
                        userInfo: userInfo,
                        is_vip_price: userInfo.is_vip_price
                    });
                    that.setTopGoodsData()
                });
            }
        },
        hide: function () {
            // 页面被隐藏
            console.log('页面被隐藏');

        },
        resize: function (size) {
            // 页面尺寸变化
            console.log('页面尺寸变化');

        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    created() {
        this.setTopGoodsData()
    },
    observers: {
        'typeVa': function (val) {
            this.setData({
                typeValue: val
            }, () => {
                this.setTopGoodsData()
            })

        },
        'num': function (val) {
            var that = this;
            let num = that.data.size
            if (num >= that.data.total) {
                return
            }
            num += 4
            that.setData({
                size: num
            })
            that.setTopGoodsData()
        },
    },
    /**
     * 组件的方法列表
     */
    methods: {
        scroll_view() {
            console.log(11111);
            var that = this;
            let num = that.data.size
            if (num >= that.data.total) {
                return
            }
            num += 6
            that.setData({
                size: num
            })
            that.setTopGoodsData()

        },
        //VIP推荐
        setTopGoodsData: function () {
            var that = this;
            var paraArr = new Array();
            paraArr['size'] = that.data.size;
            paraArr['type'] = that.data.typeValue == 'vip' ? 'hot' : 'jinhuo';
            paraArr['user_id'] = wx.getStorageSync('user_id3');
            var sign = app.signature(paraArr);
            wx.request({
                url: rootDocment + '/api_goods',
                data: {
                    size: paraArr['size'],
                    type: paraArr['type'],
                    user_id: paraArr['user_id'],
                    sign: sign
                },
                method: 'GET',
                header: {},
                success: function (res) {

                    that.setData({
                        topGoodsList: [...res.data.data],
                        total: res.data.total
                    });
                    console.log(that.data.topGoodsList, '商品信息')
                }
            })
        },

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})